package com.glory.wxmemsicpda.activity

import android.os.Handler
import android.view.View
import androidx.fragment.app.FragmentManager
import com.glory.wxmemsicpda.R
import com.glory.wxmemsicpda.adapter.FGMaterialAdapter
import com.glory.wxmemsicpda.base.App
import com.glory.wxmemsicpda.base.BaseDialogActivity
import com.glory.wxmemsicpda.bean.FGMaterialBean
import com.glory.wxmemsicpda.bean.FGMaterialItemBean
import com.glory.wxmemsicpda.bean.net.BaseResponseBean
import com.glory.wxmemsicpda.utils.MyUtils
import com.glory.wxmemsicpda.utils.NetUtils
import com.google.gson.JsonArray
import com.google.gson.JsonObject
import com.lzy.okgo.model.Response
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_SimpleDialogEntityCallBack
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralPop
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_DialogUtils
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_PopUtils
import kotlinx.android.synthetic.main.activity_data_acquisition.llMain
import kotlinx.android.synthetic.main.activity_fgfeeding_operations.*

class FGFeedingOperationsActivity : BaseDialogActivity(), View.OnClickListener {

    private var mScrapCodeList: MutableList<String>? = mutableListOf()
    private var mScrapCodePop: FreeUI_GeneralPop? = null
    private var mFGMaterialList: MutableList<FGMaterialBean.MLOTDTO>? = mutableListOf()
    private var mFGMaterialAdapter: FGMaterialAdapter? = null
    private var mlot: FGMaterialBean.MLOTDTO? = null

    override fun getLayoutResId(): Int {
        return R.layout.activity_fgfeeding_operations
    }



    override fun initView() {

        setTitle("FG投料操作")
        freeUI_titleView.setRightListener("投料") {

            val map = JsonObject()
            map.addProperty("LOTID", lotID.text.toString())
            var data = mFGMaterialAdapter!!.data
            val jsonArray = JsonArray()
            data.forEach {
                val map01 = JsonObject()
                map01.addProperty("MATERIALNAME", it!!.materialname)
                map01.addProperty("MATERIALDESC", it!!.materialdesc)
                map01.addProperty("MLOTID", it!!.mlotid)
                map01.addProperty("MLOTTYPE", "")
                map01.addProperty("STATE", it!!.state)
                map01.addProperty("MAINQTY", it!!.mainqty)
                map01.addProperty("TRANSMAINQTY", it!!.transmainqty)
                map01.addProperty("TRANSWAREHOUSERRN", "")
                map01.addProperty("RESERVED7", it!!.scrapQTY)
                map01.addProperty("RESERVED8", it!!.keyid)
                jsonArray.add(map01)
            }
            map.add("MLOTLIST", jsonArray)
            NetUtils.requestMesPosNet(this,
                "FGFEED", "MEMSIC.FGFEED",
                map,
                object : FreeUI_SimpleDialogEntityCallBack<BaseResponseBean<*>?>(this) {
                    override fun onSuccess(response: Response<BaseResponseBean<*>?>) {
                        super.onSuccess(response)
                        if (response.body()!!.isSuccess(false, supportFragmentManager)) {
                            showToast("投料成功", true)
                            finish()
                        }
                    }

                    override fun onError(response: Response<BaseResponseBean<*>?>) {
                        super.onError(response)
                        loadError(response.exception, "FGFEED")
                    }
                })
        }

        mFGMaterialAdapter = FGMaterialAdapter(mFGMaterialList, supportFragmentManager)
        fGMaterialRecycler.adapter = mFGMaterialAdapter

        flScrapCode.setOnClickListener(this)
        btn_ok.setOnClickListener(this)

    }

    override fun initData() {

        mScrapCodeList!!.add("")
        queryScrapCode()

        MyUtils.setEditTextListener(lotID, this) {

            Handler().postDelayed(Runnable { mlotID.requestFocus() }, 500)
        }

        MyUtils.setEditTextListener(mlotID, this) {

            if (lotID.text.toString().isEmpty()) {
                showToast("请扫描批次ID！", false)
                mlotID.setText("")
                return@setEditTextListener
            }

            queryMaterialMessage()
        }


    }


    private fun queryMaterialMessage() {

        val map = JsonObject()
        map.addProperty("LOTID", lotID.text.toString())
        map.addProperty("MLOTID", mlotID.text.toString())

        NetUtils.requestMesPosNet(this,
            "GETFGMATERIAL", "MEMSIC.GETFGMATERIAL",
            map,
            object : FreeUI_SimpleDialogEntityCallBack<BaseResponseBean<FGMaterialBean?>?>(this) {
                override fun onSuccess(response: Response<BaseResponseBean<FGMaterialBean?>?>) {
                    super.onSuccess(response)
                    if (response.body()!!.isSuccess(true, supportFragmentManager)) {

                        transmainQTY.setText("")
                        scrapQTY.setText("")
                        tvScrapCode.text = ""

                        mlot = response.body()!!.response.body!!.mlot
                        materialName.text = response.body()!!.response.body!!.mlot.materialname
                        mainQTY.text = response.body()!!.response.body!!.mlot.mainqty.toString()

                        Handler().postDelayed(Runnable {
                            transmainQTY.requestFocus()
                        }, 500)
                    } else {
                        mlotID.setText("")
                        Handler().postDelayed(Runnable {
                            mlotID.requestFocus()
                        }, 500)
                    }
                }

                override fun onError(response: Response<BaseResponseBean<FGMaterialBean?>?>) {
                    super.onError(response)
                    loadError(response.exception, "GETFGMATERIAL")
                }
            })


    }


    private fun queryScrapCode() {

        val map = JsonObject()

        NetUtils.requestMesPosNet(this,
            "GETFGSCRAPCODE", "MEMSIC.GETFGSCRAPCODE",
            map,
            object :
                FreeUI_SimpleDialogEntityCallBack<BaseResponseBean<FGMaterialItemBean?>?>(this) {
                override fun onSuccess(response: Response<BaseResponseBean<FGMaterialItemBean?>?>) {
                    super.onSuccess(response)
                    if (response.body()!!.isSuccess(true, supportFragmentManager)) {

                        response.body()!!.response.body!!.fgscrapcodelist.forEach {
                            mScrapCodeList!!.add(it.text)
                        }
                    }
                }

                override fun onError(response: Response<BaseResponseBean<FGMaterialItemBean?>?>) {
                    super.onError(response)
                    loadError(response.exception, "GETFGSCRAPCODE")
                }
            })


    }

    private fun showMessageDialog(message: String, fragmentManager: FragmentManager) {
        FreeApi_DialogUtils.getConfirmDialog(
            App.app, "确定", message
        ) {
            finish()
        }.show(fragmentManager, "DIALOG")

    }


    override fun getBaseEntity(): FreeUI_AddViewEntity? {
        return FreeUI_AddViewEntity("header", llMain)
    }


    override fun onClick(v: View?) {

        if (v?.id == R.id.flScrapCode) {
            if (mScrapCodePop != null) {
                if (mScrapCodePop!!.isShowing) {
                    mScrapCodePop!!.dismiss()
                }
            } else {
                mScrapCodePop = FreeApi_PopUtils.getNormalPop(
                    applicationContext, flScrapCode.width, mScrapCodeList, ivScrapCode
                ) { codeS: String?, i: Int ->
                    tvScrapCode.text = codeS
                }
            }
            FreeApi_PopUtils.setArrowShow(ivScrapCode)
            mScrapCodePop!!.showAsDropDown(flScrapCode, 0, 0)
        } else if (v?.id == R.id.btn_ok) {
//            if (tvScrapCode.text.toString().isEmpty()) {
//                showToast("请选择报废代码！", false)
//                return
//            }
            if (mlotID.text.toString().isEmpty()) {
                showToast("请输入物料批号查询物料信息！", false)
                return
            }

//            mlot = FGMaterialBean.MLOTDTO()
//            mlot!!.mainqty = 100
//            mlot!!.mlotid = "adasdasdasd"

            mlot!!.keyid = tvScrapCode.text.toString()
            mlot!!.scrapQTY = scrapQTY.text.toString()
            mlot!!.transmainqty = transmainQTY.text.toString()
            mFGMaterialList!!.add(mlot!!)
            mFGMaterialAdapter!!.notifyDataSetChanged()

        }
    }

}